﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FrameWork.Setting
{
    /// <summary>
    /// 表示一个基于INI文件操作的设定操作器
    /// </summary>
    public sealed class INISettingOperator : SettingOperatorBase
    {
        public override bool WriteSetting(string SettingParent, string SettingName, SettingValueType Type, object Value)
        {
            switch (Type)
            {
                case SettingValueType.Int32:
                    int v = (int)Value;
                    INIOperationClass.INIWriteValue(SettingFilePath, SettingParent, SettingName, v.ToString());
                    break;
                case SettingValueType.Int64:
                    long v1 = (long)Value;
                    INIOperationClass.INIWriteValue(SettingFilePath, SettingParent, SettingName, v1.ToString());
                    break;
                case SettingValueType.String:
                    string v2 = (string)Value;
                    INIOperationClass.INIWriteValue(SettingFilePath, SettingParent, SettingName, v2);
                    break;
                case SettingValueType.Bool:
                    bool v3 = (bool)Value;
                    INIOperationClass.INIWriteValue(SettingFilePath, SettingParent, SettingName, v3.ToString());
                    break;
            }
            return true;
        }

        public override object ReadSetting(string SettingParent, string SettingName, SettingValueType Type, object DefaultValue = null)
        {
            string r = INIOperationClass.INIGetStringValue(SettingFilePath, SettingParent, SettingName, "notfound");
            if (r == "notfound")
            {
                return DefaultValue;
            }
            switch (Type)
            {
                case SettingValueType.Int32:
                    return int.Parse(r);
                case SettingValueType.Int64:
                    return long.Parse(r);
                case SettingValueType.String:
                    return r;
                case SettingValueType.Bool:
                    return bool.Parse(r);
            }
            return DefaultValue;
        }

        public INISettingOperator(string SettingFilePath) : base(SettingFilePath) { }
        
    }
}
